package com.example.springbootlearn.listener;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * @author caiji Mr. Li
 * @date 2019/10/28 18:43
 */
public class AnnotatedAsyncEventListenerBootStrap {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(MyAsyncEventListener.class);

        println("Spring应用上下文正在初始化");
        context.refresh();
        context.close();
    }

    @EnableAsync
    public static class MyAsyncEventListener {
        @Async
        @EventListener(ContextRefreshedEvent.class)
        public void onEvent(ContextRefreshedEvent event) {
            println("MyAsyncEventListener : " + event.getClass().getSimpleName().toString());
        }
    }

    private static void println(String content) {
        String name = Thread.currentThread().getName();
        System.out.println("[线程" + name + " ] " + content);
    }
}
